package org.jeecg.modules.lab.items.record.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.lab.entrust.entity.LabEntrustItems;
import org.jeecg.modules.lab.entrust.entity.LabItemsRecordNorm;
import org.jeecg.modules.lab.entrust.entity.LabItemsRecordPerson;
import org.jeecg.modules.lab.entrust.entity.LabItemsRecordTarget;
import org.jeecg.modules.lab.items.temp.entity.LabItemsTempTarget;
import org.jeecg.modules.lab.items.record.mapper.LabItemsRecordTargetMapper;
import org.jeecg.modules.lab.items.record.service.ILabItemsRecordTargetService;
import org.jeecg.modules.lab.items.temp.mapper.LabItemsTempTargetMapper;
import org.jeecg.modules.lab.items.temp2.entity.LabItemsTemp2Basic;
import org.jeecg.modules.lab.items.temp2.entity.LabItemsTemp2Norm;
import org.jeecg.modules.lab.items.temp2.entity.LabItemsTemp2Target;
import org.jeecg.modules.lab.items.temp2.mapper.LabItemsTemp2TargetMapper;
import org.jeecg.modules.lab.items.vo.LabRecordTarg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @Description: 项目检测记录_检测信息
 * @Author: jeecg-boot
 * @Date: 2023-10-08
 * @Version: V1.0
 */
@Service
public class LabItemsRecordTargetServiceImpl extends ServiceImpl<LabItemsRecordTargetMapper, LabItemsRecordTarget> implements ILabItemsRecordTargetService {

    @Autowired
    private LabItemsTempTargetMapper labItemsTempTargetMapper;

    @Autowired
    private LabItemsRecordTargetMapper labItemsRecordTargetMapper;

    @Autowired
    private LabItemsTemp2TargetMapper labItemsTemp2TargetMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertTargetData(LabEntrustItems labEntrustItems) {
        String task_id = labEntrustItems.getEntrustId();
        String task_item_id= labEntrustItems.getId();
        String item_id = labEntrustItems.getItemId();
        QueryWrapper<LabItemsTempTarget> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_id", item_id)
                .orderByAsc("field_sort");
        List<LabItemsTempTarget> labItemsTempTargetList = labItemsTempTargetMapper.selectList(queryWrapper);
        for (LabItemsTempTarget labItemsTempTarget : labItemsTempTargetList) {
            // TODO: 处理每个实例 labItemsTempTarget
            LabItemsRecordTarget labItemsRecordTarget = new LabItemsRecordTarget();
            labItemsRecordTarget.setTaskId(task_id);
            labItemsRecordTarget.setTaskItemId(task_item_id);
            labItemsRecordTarget.setItemId(item_id);
            labItemsRecordTarget.setFieldName(labItemsTempTarget.getFieldName());
            labItemsRecordTarget.setFieldCode(labItemsTempTarget.getFieldCode());
            labItemsRecordTarget.setFieldValue(labItemsTempTarget.getFieldValue());
            labItemsRecordTarget.setFieldRule(labItemsTempTarget.getFieldRule());
            labItemsRecordTarget.setFieldInput(labItemsTempTarget.getFieldInput());
            labItemsRecordTarget.setFieldSort(labItemsTempTarget.getFieldSort());
            labItemsRecordTarget.setFieldType(labItemsTempTarget.getFieldType());
            labItemsRecordTarget.setFieldRow(labItemsTempTarget.getFieldRow());
            labItemsRecordTarget.setFieldCol(labItemsTempTarget.getFieldCol());
            labItemsRecordTarget.setFieldRowspan(labItemsTempTarget.getFieldRowspan());
            labItemsRecordTarget.setFieldColspan(labItemsTempTarget.getFieldColspan());
            labItemsRecordTarget.setSynFlag("0");
            labItemsRecordTargetMapper.insert(labItemsRecordTarget);
        }
    }

    @Override
    public List<LabRecordTarg> handleTargData(String entrustId,String taskItemId,String itemId) {
        List<LabRecordTarg> recordTargList = labItemsRecordTargetMapper.getRecordTargList(entrustId, itemId);
        QueryWrapper<LabItemsRecordTarget> queryWrapper = new QueryWrapper<>();
        for (LabRecordTarg labRecordTarg : recordTargList) {
            queryWrapper.clear();
            if(!"".equals(taskItemId) && taskItemId !=null){
                queryWrapper.eq("task_item_id",taskItemId);
            }
            queryWrapper.eq("field_row", labRecordTarg.getTId())
                    .eq("task_id", entrustId)
                    .eq("item_id", itemId)
                    .orderByAsc("field_sort");
            List<LabItemsRecordTarget> itemsRecordTargetList = labItemsRecordTargetMapper.selectList(queryWrapper);
            labRecordTarg.setItemsRecordTargetList(itemsRecordTargetList);
        }
        return recordTargList;
    }

    @Override
    public List<LabItemsRecordTarget> handleTargDataApp(String entrustId,String taskItemId, String itemId) {

        QueryWrapper<LabItemsRecordTarget> queryWrapper = new QueryWrapper<>();
        queryWrapper.clear();
        queryWrapper.eq("task_id", entrustId)
                .eq("item_id", itemId)
                .eq("task_item_id",taskItemId)
                .ne("field_input", "0")
                .orderByAsc("field_sort");
        List<LabItemsRecordTarget> itemsRecordTargetList = labItemsRecordTargetMapper.selectList(queryWrapper);
        return itemsRecordTargetList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(String entrustId, String itemId, List<LabRecordTarg> recordTargList) {
        //1、先根据委托/监督Id、项目id条件删除数据
        QueryWrapper<LabItemsRecordTarget> queryWrapper = new QueryWrapper<>();
        queryWrapper.clear();
        queryWrapper.eq("task_id", entrustId)
                .eq("item_id", itemId);
        labItemsRecordTargetMapper.delete(queryWrapper);
        //2、重新插入数据
        if (recordTargList != null && recordTargList.size() > 0) {
            for (LabRecordTarg recordTargs : recordTargList) {
                for (LabItemsRecordTarget targs : recordTargs.getItemsRecordTargetList()) {
                    labItemsRecordTargetMapper.insert(targs);
                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateApp(String entrustId, String itemId, List<LabItemsRecordTarget> itemsRecordTargetList) {
        //2、更新检测数据
        if (itemsRecordTargetList != null && itemsRecordTargetList.size() > 0) {
            for (LabItemsRecordTarget targs : itemsRecordTargetList) {
                labItemsRecordTargetMapper.updateById(targs);
            }
        }
    }

    @Override
    public List<LabItemsRecordTarget> byEntrustItemId(String entrustId, String itemId) {
        QueryWrapper<LabItemsRecordTarget> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("task_id", entrustId)
                .eq("item_id", itemId)
                .eq("field_input", "1");
        return labItemsRecordTargetMapper.selectList(objectQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void kyInsertTargetData(LabEntrustItems labEntrustItems) {
        String task_id = labEntrustItems.getEntrustId();
        String item_id = labEntrustItems.getItemId();
        String task_item_id = labEntrustItems.getId();

        //先删除历史数据
        QueryWrapper<LabItemsRecordTarget> wrapper = new QueryWrapper<>();
        wrapper.eq("item_id",item_id);
        labItemsRecordTargetMapper.delete(wrapper);

        QueryWrapper<LabItemsTemp2Target> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("items_id", item_id).orderByAsc("field_sort");
        List<LabItemsTemp2Target> labItemsTemp2TargetList = labItemsTemp2TargetMapper.selectList(queryWrapper);
        int size = labItemsTemp2TargetList.size();
        //先默认生成6行数据
        for (int i = 0; i < size; i++) {
            LabItemsTemp2Target target = labItemsTemp2TargetList.get(i);
            String field_name = target.getFieldName();
            //插入名称列
            LabItemsRecordTarget targeta = new LabItemsRecordTarget();
            targeta.setTaskId(task_id);
            targeta.setItemId(item_id);
            targeta.setTaskItemId(task_item_id);
            targeta.setFieldName(field_name);
            targeta.setFieldInput("0");
            targeta.setFieldSort(0 * 5 + i + 1);
            targeta.setFieldType("String");
            targeta.setFieldRow(1);
            targeta.setFieldCol(i + 1);
            targeta.setFieldRowspan(0);
            targeta.setFieldColspan(0);
            targeta.setSynFlag("0");
            labItemsRecordTargetMapper.insert(targeta);

            //插入输入列
            LabItemsRecordTarget targetb = new LabItemsRecordTarget();
            targetb.setTaskId(task_id);
            targetb.setItemId(item_id);
            targetb.setTaskItemId(task_item_id);
            targetb.setFieldName(field_name);
            targetb.setFieldCode("target_value" + (0 * 5 + i + 1));
            targetb.setFieldInput("1");
            targetb.setFieldSort(1 * 5 + i + 1);
            targetb.setFieldType("String");
            targetb.setFieldRow(2);
            targetb.setFieldCol(i + 1);
            targetb.setFieldRowspan(0);
            targetb.setFieldColspan(0);
            targetb.setSynFlag("0");
            labItemsRecordTargetMapper.insert(targetb);

            //插入输入列
            LabItemsRecordTarget targetc = new LabItemsRecordTarget();
            targetc.setTaskId(task_id);
            targetc.setItemId(item_id);
            targetc.setTaskItemId(task_item_id);
            targetc.setFieldName(field_name);
            targetc.setFieldCode("target_value" + (1 * 5 + i + 1));
            targetc.setFieldInput("1");
            targetc.setFieldSort(2 * 5 + i + 1);
            targetc.setFieldType("String");
            targetc.setFieldRow(3);
            targetc.setFieldCol(i + 1);
            targetc.setFieldRowspan(0);
            targetc.setFieldColspan(0);
            targetc.setSynFlag("0");
            labItemsRecordTargetMapper.insert(targetc);

            //插入输入列
            LabItemsRecordTarget targetd = new LabItemsRecordTarget();
            targetd.setTaskId(task_id);
            targetd.setItemId(item_id);
            targetd.setTaskItemId(task_item_id);
            targetd.setFieldName(field_name);
            targetd.setFieldCode("target_value" + (2 * 5 + i + 1));
            targetd.setFieldInput("1");
            targetd.setFieldSort(3 * 5 + i + 1);
            targetd.setFieldType("String");
            targetd.setFieldRow(4);
            targetd.setFieldCol(i + 1);
            targetd.setFieldRowspan(0);
            targetd.setFieldColspan(0);
            targetd.setSynFlag("0");
            labItemsRecordTargetMapper.insert(targetd);

            //插入输入列
//            LabItemsRecordTarget targete = new LabItemsRecordTarget();
//            targete.setTaskId(task_id);
//            targete.setItemId(item_id);
//            targete.setFieldName(field_name);
//            targete.setFieldCode("target_value" + (3 * 5 + i + 1));
//            targete.setFieldInput("1");
//            targete.setFieldSort(4 * 5 + i + 1);
//            targete.setFieldType("String");
//            targete.setFieldRow(5);
//            targete.setFieldCol(i + 1);
//            targete.setFieldRowspan(0);
//            targete.setFieldColspan(0);
//            targete.setSynFlag("0");
//            labItemsRecordTargetMapper.insert(targete);

            //插入输入列
//            LabItemsRecordTarget targetf = new LabItemsRecordTarget();
//            targetf.setTaskId(task_id);
//            targetf.setItemId(item_id);
//            targetf.setFieldName(field_name);
//            targetf.setFieldCode("target_value" + (4 * 5 + i + 1));
//            targetf.setFieldInput("1");
//            targetf.setFieldSort(5 * 5 + i + 1);
//            targetf.setFieldType("String");
//            targetf.setFieldRow(6);
//            targetf.setFieldCol(i + 1);
//            targetf.setFieldRowspan(0);
//            targetf.setFieldColspan(0);
//            targetf.setSynFlag("0");
//            labItemsRecordTargetMapper.insert(targetf);
        }

        LabItemsRecordTarget targetg = new LabItemsRecordTarget();
        targetg.setTaskId(task_id);
        targetg.setItemId(item_id);
        targetg.setTaskItemId(task_item_id);
        targetg.setFieldName("备注/结论");
        targetg.setFieldInput("0");
        targetg.setFieldSort(size * 6 + 1);
        targetg.setFieldType("String");
        targetg.setFieldRow(7);
        targetg.setFieldCol(1);
        targetg.setFieldRowspan(0);
        targetg.setFieldColspan(0);
        targetg.setSynFlag("0");
        labItemsRecordTargetMapper.insert(targetg);

        LabItemsRecordTarget targeth = new LabItemsRecordTarget();
        targeth.setTaskId(task_id);
        targeth.setItemId(item_id);
        targeth.setTaskItemId(task_item_id);
        targeth.setFieldName("备注/结论");
        targeth.setFieldCode("target_value" + (size * 5 + 1));
        targeth.setFieldInput("1");
        targeth.setFieldSort(size * 6 + 1);
        targeth.setFieldType("String");
        targeth.setFieldRow(7);
        targeth.setFieldCol(2);
        targeth.setFieldRowspan(0);
        targeth.setFieldColspan(size - 1);
        targeth.setSynFlag("0");
        labItemsRecordTargetMapper.insert(targeth);
    }

    @Override
    public void deleteByTaskId(String taskId) {
        labItemsRecordTargetMapper.deleteByTaskId(taskId);
    }
}
